<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog=true">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table
        :data="roleList"
        style="width: 100%"
      >
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色" prop="name">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else>{{ row.name }}</span>
          </template>

        </el-table-column>
        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              size="mini"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>
              {{ row.state === 1 ? '已启用' : '未启用' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" prop="description">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="editOk(row)">确认</el-button>
              <el-button type="text" size="small" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="OpenDialogAssign(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="editRow(row)">编辑</el-button>
              <!-- <el-button type="text" size="small">删除</el-button> -->
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delOk(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>

      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          background
          :current-page="page"
          :page-sizes="[5,10,15]"
          layout=" prev, pager, next ,sizes"
          :total="total"
          :page-size="pagesize"
          @current-change="hCurrentChange"
          @size-change="hSizeChange"
        />
      </el-row>
    </div>
    <!-- 添加角色 -->
    <el-dialog width="500px" title="新增角色" :visible.sync="showDialog">
      <!-- 表单内容 -->
      <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item label="启用" prop="state">
          <el-switch
            v-model="formData.state"
            size="mini"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="addRoles">确定</el-button>
              <el-button size="mini" @click="showDialog=false">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
    <assign-permission ref="refPerm" :show-dialog-assign.sync="showDialogAssign" :cur-id="curId" />
  </div>
</template>
<script>
import { getRoles, addRoles, editRoles, delRoles } from '@/api/role'
// import AssignPermission from './components/assignPermission.vue'
import AssignPermission from './AssignPermission.vue'
export default {
  name: 'Role',
  components: { AssignPermission },
  data() {
    return {
      showDialogAssign: false,
      formData: {
        name: '',
        state: 1,
        description: ''
      },
      curId: -1,
      roleList: [],
      total: 0,
      pagesize: 5,
      page: 1,
      showDialog: false,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '角色名称的长度为2-10个字符', trigger: 'blur' }],
        // state: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    OpenDialogAssign(id) {
      this.curId = id
      this.showDialogAssign = true
      this.$nextTick(() => {
        this.$refs.refPerm.getPermiss()
      })
    },
    // 删除
    async  delOk(id) {
      await delRoles(id)
      this.$message.success('删除成功')
      if (this.roleList.length === 1 && this.page > 1) {
        this.page--
      }
      this.getRoles()
    },
    // 确认编辑
    async editOk(row) {
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.error('不可为空')
        return
      }
      await editRoles(row.editRow)
      this.$message.success('更新成功')
      this.getRoles()
      // eslint-disable-next-line require-atomic-updates
      // row.isEdit = false
    },
    // 行内编辑
    editRow(row) {
      row.isEdit = true
    },
    // 添加新角色封装
    async doAdd() {
      await addRoles(this.formData)
      this.showDialog = false
      this.$message.success('添加成功')
      this.$refs.formData.resetFields()
      // console.log(res)
      // this.roleList.unshift(res.data)
      this.total = this.total + 1
      this.page = Math.ceil((this.total + 1) / this.pagesize)
      this.getRoles()
    },
    // 调用 添加新角色函数
    addRoles() {
      this.$refs.formData.validate(valid => {
        valid && this.doAdd()
      })
    },
    // 修改每页数据数量
    hSizeChange(pages) {
      this.pagesize = pages
      this.page = 1

      this.getRoles()
    },
    // 翻页
    hCurrentChange(page) {
      // alert(page)
      this.page = page
      this.getRoles()
    },
    // 初始化渲染封装
    async getRoles() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      // console.log(res)

      this.roleList = res.data.rows
      this.total = res.data.total
      this.roleList.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          id: item.id,
          state: item.state,
          description: item.description,
          name: item.name
        })
      })
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
